<?php 
	class PacketAction extends CommonAction{
		public function __construct(){
		parent::__construct();
		$this->checkAuth();
		$this->checkReal();

		$sys=M("Sys")->where("id=1")->find();
		if($sys['hongbao']==0){
			$this->error("暂未开放");
		}
	}
	//空操作
	Public function _empty(){
	    header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
	    $this->display("Public:404");
	}
	    /**
	     * 红包首页
	     */
	    public function index(){
	       $packet = M('Packet'); // 实例化User对象
	        import('ORG.Util.Page');// 导入分页类
	        $count      = $packet->count();// 查询满足要求的总记录数
	        $Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
	        $show       = $Page->show();// 分页显示输出
	        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
	        $list = $packet->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	        foreach ($list as $k=>$v){
	         $count= M("News")->where("packet_id='{$v['id']}'")->count();
	         $list[$k]['count']=$v['num']-$count;
	         
	         
	         $list[$k]['button']=$this->button_style($v['id'],$v['status']);
	        }

	        
	       $news= M("News")->order("add_time desc")->limit(10)->select();
	       $this->assign("news",$news);
	       $this->assign('list',$list);// 赋值数据集
	       $this->assign('page',$show);// 赋值分页输出
	    	$this->display('./Tpl/Home/packet_index.html');
	    }

	    public function packet_add(){
        $user=M("User")->where("id = '{$_SESSION['USER_KEY_ID']}'")->find();
        if($user['paypwd']!=md5($_POST['password'])){//判断支付密码是否正确
            $arr['info']="支付密码错误";
            $arr['status']=0;
            $this->ajaxReturn($arr);
        }
        if($user['xnb']<$_POST['money']){
	       $arr['info']="您的金币余额不足，无法发红包";
	       $arr['status']=2;
	       $this->ajaxReturn($arr);
        }
        
	      $data['num']=$_POST['num'];//红包数量  
          $data['type']=$_POST['type'];//获取币种类型
          $data['money']=$_POST["money"];//获取金钱数量
          $data['content']=$_POST["content"];//获取红包寄语  
          $data['uid']=$_SESSION['USER_KEY_ID'];//用户id
          $data['add_time']=time();//添加时间
          $data['status']=1;//添加初始状态
         $re= M("Packet")->add($data);
         if($re){
             M("User")->where("id='{$_SESSION['USER_KEY_ID']}'")->setDec("xnb",$_POST['money']);//给发放者减钱
             $arr['status']=1;
             $arr['info']="发放成功";
             $this->ajaxReturn($arr);
         }else{
             $arr['status']=3;
             $arr['info']="发放失败";
             $this->ajaxReturn($arr);
         }
         
        
        
	    }
	    
	    
	    
	
	    /**
	     * 我的抢购
	     */
	    public function my_packet(){
	        $news = M('News'); // 实例化User对象
	        $where['uid']=intval($_SESSION['USER_KEY_ID']);//用户id 用户查询自己
	        import('ORG.Util.Page');// 导入分页类
	        $count      = $news->where($where)->count();// 查询满足要求的总记录数
	        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
	        $show       = $Page->show();// 分页显示输出
	        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
	        $list = $news->where($where)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	        $this->assign('list',$list);// 赋值数据集
	        $this->assign('page',$show);// 赋值分页输出
	    
	    	$this->display('./Tpl/Home/my_packet.html');
	    }
	     
	    /**
	     * 我的发布
	     */
	    public function my_packet_list(){
	       $packet = M('Packet'); // 实例化User对象
	        import('ORG.Util.Page');// 导入分页类
	        $where['uid']=intval($_SESSION['USER_KEY_ID']);//用户id 用户查询自己
	        $count      = $packet->where($where)->count();// 查询满足要求的总记录数
	        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
	        $show       = $Page->show();// 分页显示输出
	        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
	        $list = $packet->where($where)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	     foreach ($list as $k=>$v){
	          $count= M("News")->where("packet_id='{$v['id']}'")->count();
	          $list[$k]['count']=$v['num']-$count;
	        }
	        $this->assign('list',$list);// 赋值数据集
	        $this->assign('page',$show);// 赋值分页输出
	    	$this->display('./Tpl/Home/my_packet_list.html');
	    }
	     /**
	      * 按钮状态
	      * @param unknown $id
	      * @param unknown $status
	      * @return void|string
	      */
	    private function button_style($id,$status){
	        switch ($status){
	            case 1:$button='<button class="btn btn-danger" type="button" onclick="submitForm(this)" >可领取</button>';break;
	            case 0:$button='<button class="btn btn-primary" type="button" >已抢光</button>';break;
	        }
	        $re=M("News")->where("packet_id='$id' and uid='{$_SESSION['USER_KEY_ID']}'")->find();
	        if(!empty($re)){
	            $button='<button class="btn btn-default" type="button">已领取</button>';
	            return $button;
	        }
	        return $button;
	    }
	    
	    
	    public function lingqu(){
	        $id=intval($_POST['id']);//获取id号
	        $packet=M("Packet")->where("id='$id'")->find();
	        $news=M("News")->where("packet_id='$id' and uid = '{$_SESSION['USER_KEY_ID']}'")->find();
	        $count=M("News")->where("packet_id='$id'")->count();
	        if($packet['num']<=$count){
	           M("Packet")->where("id='$id'")->setField("stauts",0);//设置成已抢光
	            $arr['info']="已抢光";
	            $arr['status']=0;
	            $this->ajaxReturn($arr);
	        }
	        if(!empty($news)){
	            $arr['info']="已抢，请勿重复抢";
	            $arr['status']=2;
	            $this->ajaxReturn($arr);
	        }
	        $money=$packet['money']/$packet['num'];//一份的价格
	        $data['packet_id']=$id;
	        $data['uid']=$_SESSION['USER_KEY_ID'];
	        $data['fafang_uid']=$packet['uid'];
	        $data['add_time']=time();
	        $data['money']=$money;
	        $data['status']=1;
	        $re=M("News")->add($data);
	        if($re){
	           M("User")->where("id='{$_SESSION['USER_KEY_ID']}'")->setInc("xnb",$money);//加钱
	           $arr['info']="成功抢到红包";
	            $arr['status']=1;
	           $this->ajaxReturn($arr);
	        }else{
	            $arr['info']="红包抢购失败";
	            $arr['status']=3;
	            $this->ajaxReturn($arr);
	        }
	        
	    }
}

	
?>